package com.zhaoyang.servlet;

import com.zhaoyang.util.CpachaUtil;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * @author 赵扬
 * @time 2020/4/28 13:02
 */
@WebServlet("/cpachaServlet")
public class CpachaServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String method = request.getParameter("method");

        if ("loginCapcha".equals(method)) {
            CpachaUtil cpachaUtil = new CpachaUtil();
            String generatorVcode = cpachaUtil.generatorVCode();
            request.getSession().setAttribute("loginCapcha", generatorVcode);
            BufferedImage img = cpachaUtil.generatorVCodeImage(generatorVcode, true);
            ImageIO.write(img, "gif", response.getOutputStream());
            return;
        }
        response.getWriter().write("error method");
    }
}
